import 'package:favor_cate/core/model/meal_model.dart';
import 'package:favor_cate/core/viewmodel/favor_meal_veiw_model.dart';
import 'package:favor_cate/ui/pages/detail/detail.dart';
import 'package:favor_cate/ui/widgets/operation_item.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:favor_cate/core/extension/int_extension.dart';
import 'package:provider/provider.dart';

class MealItem extends StatelessWidget {
  final MealModel _meals;
  MealItem(this._meals);

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: () {
        Navigator.of(context).pushNamed(DetailScreen.routeName, arguments: _meals);
      },
      child: Card(
        margin: EdgeInsets.all(20.px),
        elevation: 10.px,
        shape: BeveledRectangleBorder(borderRadius: BorderRadius.circular(12.px)),
        child: Column(
          children: [
            buildBasicInfo(context),
            buildOperationInfo()
          ],
        ),
      ),
    );
  }

  Widget buildBasicInfo(BuildContext ctx) {
    return Stack(
      children: [
        ClipRRect(
            borderRadius: BorderRadius.only(
              topLeft: Radius.circular(12.px),
              topRight: Radius.circular(12.px),
            ),
            child: Image.network(
              _meals.imageUrl,
              width: double.infinity,
              height: 350.px,
              fit: BoxFit.cover,
            )),
        Positioned(
          right: 10.px,
          bottom: 10.px,
          child: Container(
            width: 500.px,
            padding: EdgeInsets.symmetric(horizontal: 10, vertical: 5),
            decoration: BoxDecoration(
              color: Colors.black54,
              borderRadius: BorderRadius.circular(12.px)
            ),
            child: Text(_meals.title, style: Theme.of(ctx).textTheme.headline3!.copyWith(
              color: Colors.white
            ),),
          ),
        )
      ],
    );
  }

  Widget buildOperationInfo() {
    return Padding(
      padding: EdgeInsets.all(32.px),
      child: Row(
        mainAxisAlignment:MainAxisAlignment.spaceAround,
        children: [
          OperationItem(Icon(Icons.schedule), "${_meals.duration}分钟"),
          OperationItem(Icon(Icons.restaurant), "${_meals.complexityStr}"),
          buildFavorItem(),
        ],
      ),
    );
  }

  Widget buildFavorItem() {
    return Consumer<FavorMealViewModel>(
        builder: (ctx, model, child) {
          final iconData = model.isFavor(_meals)? Icons.favorite: Icons.favorite_border;
          final iconColor = model.isFavor(_meals)? Colors.red: Colors.black;
          final title = model.isFavor(_meals)? "已收藏": "未收藏";
          return GestureDetector(
            child: OperationItem(Icon(iconData, color: iconColor,), title),
            onTap: () => model.isFavor(_meals)? model.removeFavor(_meals): model.addFavor(_meals) ,
          );
        }
    );
  }
}


